package com.yuejian_06.yuejian_06.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.activity.PostListActivity;
import com.yuejian_06.yuejian_06.adapter.ContestAdapter;
import com.yuejian_06.yuejian_06.adapter.PicAdapter;
import com.yuejian_06.yuejian_06.bean.ContestBean;
import com.yuejian_06.yuejian_06.bean.PicBean;
import com.yuejian_06.yuejian_06.model.HomeModel;
import com.yuejian_06.yuejian_06.model.MeModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContestFragment extends Fragment {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout fragment_contest;

    public ContestFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_contest, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.contestlist_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        HomeModel homeModel = new HomeModel();
        homeModel.getContestList(meListener);

        //下拉刷新
        fragment_contest = (SwipeRefreshLayout) view.findViewById(R.id.fragment_contest);
        fragment_contest.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override public void onRefresh() {
                //重新获取数据
                HomeModel homeModel = new HomeModel();
                homeModel.getContestList(meListener1);
                //获取完成
                fragment_contest.setRefreshing(false);
            }
        });

        return view;
    }

    private MeListener<List<ContestBean>> meListener = new MeListener<List<ContestBean>>() {
        @Override
        public void onResponse(List<ContestBean> o) {
            List<ContestBean> res=o;
            List<ContestBean> mDataList =(List) res;
            recyclerView.setAdapter(new ContestAdapter(getActivity(),mDataList));
        }

        @Override
        public void onFail(String msg) {

        }
    };


    private MeListener<List<ContestBean>> meListener1 = new MeListener<List<ContestBean>>() {
        @Override
        public void onResponse(List<ContestBean> o) {
            List<ContestBean> res=o;
            List<ContestBean> mDataList =(List) res;
            recyclerView.setAdapter(new ContestAdapter(getActivity(),mDataList));
            Toast.makeText(getActivity(),"比赛列表刷新成功",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };

}
